package com.zshield.entry;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;

public class NetDenyEntry implements EntryInterface<NetDenyEntry> {
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("USERNAME")
    private String userName;
    @SerializedName("RULENAME")
    private String ruleName;
    @SerializedName("EXE_NAME")
    private String exeName;
    @SerializedName("VIOLATION_TIMES")
    private int violationTimes;
    @SerializedName("TARGET_NAME")
    private  String targetName;
    @SerializedName("IP")
    private String ip;
    @SerializedName("MAC")
    private String mac;
    @SerializedName("COMPUTER")
    private String computer;
    @SerializedName("LEVEL")
    private String level;
    @SerializedName("FORMAT")
    private String format;
    @SerializedName("TIME")
    private String time;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("PTYPE")
    private String ptype;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("TIME_hms")
    private String timeHms;

    public NetDenyEntry(JsonObject obj) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        userName = obj.get("USERNAME").getAsString();
        ruleName = obj.get("RULENAME").getAsString();
        exeName = obj.get("EXE_NAME").getAsString();
        //升级的情况下如果没有COUNT字段则COUNT=1,如果有COUNT则获取COUNT的值
        violationTimes = obj.get("COUNT") != null ? obj.get("COUNT").getAsInt() : 1;
        targetName = obj.get("TARGET_NAME").getAsString();
        ip = obj.get("IP").getAsString();
        mac = obj.get("MAC").getAsString();
        computer = obj.get("COMPUTER").getAsString();
        level = "ALARM";
        format = "SENSOR_NETWORK_ABNORMAL";
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "NetDeny";
    }

    public void update(NetDenyEntry that) {
        this.violationTimes = this.violationTimes + that.violationTimes;
        this.time = that.time;
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    public String getDocId() {
        return "NetDeny~" + sensorId + "~" + userName + "~" + ruleName + "~" + exeName + "~" + targetName + "~" + dayTime;
    }

}
